mui.plusReady(function() {
	var pic = localStorage.getItem('pic');
	userInfo = localStorage.getItem('userInfo');
	//头像
	if (pic) {
		$("#pic").attr('src', pic);
	} else {
		$("#pic").attr('src', 'image/pic.png');
	}

	/**
	 * 我的收藏
	 */
	userInfo = JSON.parse(userInfo);
	if (userInfo) {
		function total(id) {
			var uid = id || userInfo.uid;
			mui.post(http('Api/User/index'), {
				uid: uid
			}, function(data) {
				localStorage.setItem('total', data);
				var total = localStorage.getItem('total');
				$("#total").html(total);
			}, 'json');
		}
		total();
	}


	//评论中心发过来的数据
	window.addEventListener('total', function(event) {
		var uid = event.detail.uid;
		total(uid);
	})

	$("#pic").on('tap', function() {
			plus.nativeUI.actionSheet({
				title: '亲！换个头像吧！',
				cancel: '关闭',
				buttons: [{
					title: '拍照'
				}, {
					title: '从相册选择'
				}]
			}, function(e) {
				var i = e.index;
				if (i == 1) {
					photo();
				}
				if (i == 2) {
					gallery();
				}
			})
		})
		/**
		 *	拍照
		 * @index主次摄像头(0,1)
		 * @res摄像头支持的摄像分辨率
		 * @fmt摄像头支持的拍照文件格式
		 * @captureImage拍照
		 */

	function photo() {
			var index = 1;
			var cmr = plus.camera.getCamera(index);
			var res = cmr.supportedVideoResolutions[0];
			var fmt = cmr.supportedImageFormats[0];
			cmr.captureImage(function(path) {
				flie(path);
			}, function(error) {
				plus.nativeUI.toast(error.message);
			}, {
				resolution: res,
				format: fmt,
				popover: {
					top: '50px'
				}
			})
		}
		/**
		 * 从相册选择
		 */

	function gallery() {
			plus.gallery.pick(function(path) {
				flie(path);
			})
		}
		/**
		 * 照片裁剪
		 */
	var articlePage = null;

	function chaijian(path) {
		plus.io.resolveLocalFileSystemURL(path, function(entry) {
			var localUrl = entry.toLocalURL();
			plus.webview.create('caijian.html', 'caijian.html', {
				scrollIndicator: 'none'
			});
			if (!articlePage) {
				articlePage = plus.webview.getWebviewById('caijian.html');
			}
			mui.fire(articlePage, 'localUrl', {
				localUrl: localUrl
			})
			plus.webview.show('caijian.html', 'zoom-fade-out', 300);
		})
	}


	//照片上传
	function flie(path) {
			plus.io.resolveLocalFileSystemURL(path, function(entry) {
				var localUrl = entry.toLocalURL();
				var img = new Image();
				img.src = localUrl; // 传过来的图片路径在这里用。
				img.onload = function() {
					var that = this;
					//生成比例 
					var w = that.width,
						h = that.height,
						scale = w / h;
					w = 80 || w; //480  你想压缩到多大，改这里
					h = w / scale;

					//生成canvas
					var canvas = document.createElement('canvas');

					var ctx = canvas.getContext('2d');

					$(canvas).attr({
						width: w,
						height: h
					});

					ctx.drawImage(that, 0, 0, w, h);

					var base64 = canvas.toDataURL('image/jpeg', 1 || 0.8); //1最清晰，越低越模糊。有一点不清楚这里明明设置的是jpeg。弹出 base64 开头的一段 data：image/png;却是png。哎开心就好，开心就好
					upload(base64);
				}
			})
		}
		//上传服务器

	function upload(base64) {
		var wt = plus.nativeUI.showWaiting();
		mui.post(http('upload'), {
			data: base64
		}, function(data) {
			var path = data;
			path = path.replace(/\\/g,'');
			wt.close();   
			localStorage.setItem('pic', path);
			var pic = localStorage.getItem('pic');
			plus.nativeUI.toast('修改成功！');
			$("#pic").attr('src', pic);
		}, 'json');
	}

	/**
	 * 退出登陆
	 */
	if (!userInfo) {
		$(".btn").hide();
		$(".login").show();
	} else {
		$(".btn").show();
		$(".login").hide();
	}
	$(".btn").on('tap', function() {
		$("#total").html('0');
		localStorage.removeItem('userInfo');
		localStorage.removeItem('total');
		plus.nativeUI.toast('退出成功!');
		//清掉变量
		userInfo = null;
		$(".login").show();
		$(this).hide();
	})

	$(".login").on('tap', function() {
		plus.webview.create('login.html', 'login.html', {
			scrollIndicator: 'none'
		})
		setTimeout(function() {
			plus.webview.show('login.html', 'zoom-fade-out', 300);
		}, 300)
	})

	//登陆页面才会触发 
	window.addEventListener('success', function(event) {
		var uid = event.detail.uid;
		$(".login").hide();
		$(".btn").show();
		//重新获得user的值
		userInfo = JSON.parse(localStorage.getItem('userInfo'));
		total(uid);
	})

	/**
	 * 收藏列表
	 */
	$("#list").on('tap', function() {
		var targetPage = null;
		if (userInfo == null) return;
		var uid = userInfo.uid;
		plus.webview.create('userlist.html', 'userlist.html', {
			scrollIndicator: 'none'
		});
		//传递用户ID
		if (!targetPage) { 
			targetPage = plus.webview.getWebviewById('userlist.html');
		}
		plus.nativeUI.showWaiting();
		mui.fire(targetPage, 'uid', {
			uid: uid
		});
		plus.webview.show('userlist.html', 'slide-in-bottom', 300);
	})
})